package com.twotiger.and.activity.base;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import app.twotiger.p2p.R;

import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.user.LoginPage;
import com.twotiger.and.util.LogUtil;

/**
 * handler
 * 
 * @Description: 
 * 消息值 3000 以上是系统保留值各自禁止使用
 * @Author douxy
 * @Date 2015年8月13日 下午12:50:50
 */
public class BaseHandler extends Handler {

    private BaseActivity mActivity;
    /**校验手势*/
    public static final int TIME_TO_VAILD_GESTURE = 3001;
    /**token失效*/
    public static final int TOKEN_TIME_OUT = 3002;
    /**系统维护中*/
    public static final int SYSTEM_UPGRADE = 3003;
    /**验证token是否失效*/
    public static final int CHECKTOKENSUC = 3004;  
    public static final int CHECKTOKENFAI = 3005;
    /**服务器错误*/
    public static final int SYSTEM_ERROR = 3006;
    /**token无效*/
    public static final int TOKEN_INVALID = 3007;
    
    

    public static final int TIME_TO_VAILD_GESTURE_Delayed = 300;

    public BaseHandler(BaseActivity mActivity) {
        this.mActivity = mActivity;
    }

    @Override
    public void handleMessage(Message msg) {
        LogUtil.info(BaseHandler.class, getClass() + " handleMessage : " + msg.what);
        switch (msg.what) {
        case TIME_TO_VAILD_GESTURE:
            if (mActivity.needValidGesture()) {
                mActivity.checkToken();
            }
            break;
        case TOKEN_TIME_OUT:   //token失效跳到登录界面
            mActivity.application.logout();
            Intent in1 = new Intent(mActivity, LoginPage.class);
            mActivity.startNewActivity(in1, R.anim.slide_in_from_bottom, R.anim.silent_anim, false);
            break;
        case SYSTEM_UPGRADE:  //服务器升级时，四大标签页显示
            mActivity.showToast("系统正在升级...");
            if(mActivity instanceof HomeActivity){
                //homeactivity中发生系统升级，无需销毁本身！
                mActivity.startNewActivity(new Intent(mActivity, HomeActivity.class).putExtra("SYSTEM_UPGRADE", BaseHandler.SYSTEM_UPGRADE), R.anim.push_left_in, R.anim.push_left_out, false);
            }else {
                //非homeactivity中发生系统升级，需要销毁本身，然后启动homeactivity！
                mActivity.startNewActivity(new Intent(mActivity, HomeActivity.class).putExtra("SYSTEM_UPGRADE", BaseHandler.SYSTEM_UPGRADE), R.anim.push_left_in, R.anim.push_left_out, true);
            }
            break;
        case SYSTEM_ERROR:  //服务器错误
            mActivity.showToast("服务器错误...");
            break;
        case TOKEN_INVALID:  //token无效跳到登录界面
            mActivity.application.logout();
            Intent in2 = new Intent(mActivity, LoginPage.class);
            mActivity.startNewActivity(in2, R.anim.slide_in_from_bottom, R.anim.silent_anim, false);
            break;
        }

        super.handleMessage(msg);
    }

}
